import { Node } from "cc";
import { FishSchool } from "./FishSchool";

export class FishUtil {
    public static createFish(fishes:any[]){
        let school = new FishSchool(fishes, [[-10, 10], [-10, 10]], [[-1, 1], [-1, 1]]);
        return school;
    }

    public static func(x: number[]): number {
        return Math.pow(x[0], 2) + Math.pow(x[1], 2);
    }
}

// class Fish extends cc.Sprite {
//     position: number[];
//     velocity: number[];
//     fitness: number;

//     constructor(position: number[], velocity: number[]) {
//         super();
//         this.position = position;
//         this.velocity = velocity;
//         this.fitness = Infinity;
//     }

//     updateVelocity(bestFish: Fish) {
//         let newVelocity = [];
//         for (let i = 0; i < this.position.length; i++) {
//             let r = Math.random();
//             let c = 2 * r * (r - 1);
//             newVelocity.push(this.velocity[i] + c * (this.position[i] - bestFish.position[i]));
//         }
//         this.velocity = newVelocity;
//     }

//     updatePosition() {
//         let newPosition = [];
//         for (let i = 0; i < this.position.length; i++) {
//             newPosition.push(this.position[i] + this.velocity[i]);
//         }
//         this.position = newPosition;
//         this.x = this.position[0];
//         this.y = this.position[1];
//     }

//     calculateFitness(func: Function) {
//         this.fitness = func(this.position);
//     }
// }

// class FishSchool {
//     fishes: Fish[];
//     bestFish: Fish;

//     constructor(size: number, positionRange: number[][], velocityRange: number[]) {
//         this.fishes = [];
//         for (let i = 0; i < size; i++) {
//             let position = [];
//             let velocity = [];
//             for (let j = 0; j < positionRange.length; j++) {
//                 position.push(Math.random() * (positionRange[j][1] - positionRange[j][0]) + positionRange[j][0]);
//                 velocity.push(Math.random() * (velocityRange[j][1] - velocityRange[j][0]) + velocityRange[j][0]);
//             }
//             this.fishes.push(new Fish(position, velocity));
//         }
//         this.bestFish = this.fishes[0];
//     }

//     update(func: Function) {
//         for (let i = 0; i < this.fishes.length; i++) {
//             this.fishes[i].updateVelocity(this.bestFish);
//             this.fishes[i].updatePosition();
//             this.fishes[i].calculateFitness(func);
//             if (this.fishes[i].fitness < this.bestFish.fitness) {
//                 this.bestFish = this.fishes[i];
//             }
//         }
//     }
// }

// // 在Cocos Creator中使用Fish和FishSchool类
// let school = new FishSchool(10, [[-10, 10], [-10, 10]], [[-1, 1], [-1, 1]]);

// cc.director.getScene().on('update', function(dt) {
//     school.update(func);
// });